package com.adee.gulimall.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;

/**
 * 限流全局过滤器
 *
 *
 */
@Slf4j
//@Component
public class Global_2_LimitFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        URI uri = request.getURI();
        log.info("uri:{}", uri);
        String userAgent = request.getHeaders().getFirst("User-Agent");
        log.info("User-Agent:{}", userAgent);


//        if(StringUtils.isEmpty(id)){
//            log.info("用户名不存在，非法请求！");
//            //如果username为空，返回状态码为407，需要代理身份验证
//            exchange.getResponse().setStatusCode(HttpStatus.PROXY_AUTHENTICATION_REQUIRED);
//            // 后置过滤器
//            return exchange.getResponse().setComplete();
//        }
        return chain.filter(exchange);
    }

    /**
     * 过滤器的优先级，数字越小优先级越高
     * @return
     */
    @Override
    public int getOrder() {
        return OrderNum.LimitFilterOrder;
    }
}
